package cn.gson.springboot.model.service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;

import cn.gson.springboot.model.dao.CkDAO;
import cn.gson.springboot.model.dao.DrugsYkDao;
import cn.gson.springboot.model.dao.InvoceDAO;
import cn.gson.springboot.model.dao.PrescriptionDAO;
import cn.gson.springboot.model.dao.SpenddetaiDAO;
import cn.gson.springboot.model.mapper.ChufangMapper;
import cn.gson.springboot.model.mapper.PrescriptionPXMapper;
import cn.gson.springboot.model.pojos.Ck;
import cn.gson.springboot.model.pojos.DepartmentsPx;
import cn.gson.springboot.model.pojos.DrugsYk;
import cn.gson.springboot.model.pojos.Fanku;
import cn.gson.springboot.model.pojos.Fankudetai;
import cn.gson.springboot.model.pojos.Invoice;
import cn.gson.springboot.model.pojos.PrescriptionPx;
import cn.gson.springboot.model.pojos.Spenddetai;
import cn.gson.springboot.model.vo.ChufangVO;

@Service
@Transactional(rollbackFor = Exception.class)
public class ChufangService {
	@Autowired
	private ChufangMapper tm;
	@Autowired
	private DrugsYkDao ddao;
	@Autowired
	private PrescriptionDAO pm;
	@Autowired
	private CkDAO ck;
	@Autowired
	private SpenddetaiDAO sm;
	@Autowired
	private InvoceDAO im;
	@Autowired
	private PrescriptionPXMapper ps;

	// 查询所有药品
	public List<Map<String, Object>> cxsy() {
		List<Map<String, Object>> ll = tm.cfsy();
		return ll;
	}
	// 模糊查询药品
		public List<Map<String, Object>> cxmhsy(String a) {
			List<Map<String, Object>> ll = tm.cfmhsy(a);
			return ll;
		}
		// 模糊查询药品
		public List<Map<String, Object>> bmmhcx(String a) {
			List<Map<String, Object>> ll = tm.bmmhcx(a);
			return ll;
		}

	// 查询所有药品
	public List<Map<String, Object>> bmsy() {
		List<Map<String, Object>> ll = tm.bmsy();
		return ll;
	}

	// 查询处方详情
	public List<Map<String, Object>> cxxq(int a) {
		List<Map<String, Object>> ll = tm.cfxq(a);
		return ll;
	}

	// 查询部门详情
	public List<Map<String, Object>> bmxq(int a) {
		List<Map<String, Object>> ll = tm.bmxq(a);
		return ll;
	}

	public void tjxq(ChufangVO svo) {
		System.out.println("到了这一步了");
		List<Spenddetai> details = svo.getCfdetails();
		System.out.println("到了这一步了" + details.get(0).getPrescriptionPx().getPrescriptionId());
		// 循环
		for (Spenddetai pur : details) {
			System.out.println("我到了循环了");
			// 添处方表详情信息
			PrescriptionPx save = pm.findById(pur.getPrescriptionPx().getPrescriptionId()).get();
			System.out.println("处方编号" + save.getPrescriptionId());
			pur.setPrescriptionPx(save);

			String bz = pur.getDeRemarkZc();
			pur.setDeRemarkZc(bz);
			System.out.println("我到了备注");
			int id = pur.getDrugsYk().getDrugId();
			System.out.println("id是" + id);
			Ck c = ck.kmpccx(id, pur.getDrugsPc());

			System.out.println(c.getDrugs_Stock_kc() + "--------");
			c.setDrugs_Stock_kc(c.getDrugs_Stock_kc() - pur.getDrugsNumberZc());
			ck.save(c);
			System.out.println("修改成功");

			// 查询药品
			DrugsYk drug = ddao.findById(pur.getDrugsYk().getDrugId()).get();

			// 重新赋值一下药品对象
			pur.setDrugsYk(drug);
			sm.save(pur);
		}
	}

	// 处方自动发药
	public void zdfy(ChufangVO svo) {

		System.out.println("到了这一步了");
		List<Spenddetai> details = svo.getCfdetails();

		for (Spenddetai pur : details) {

			// 根据药品id查询所有批次存入数组
			System.out.println("id" + pur.getDrugsYk().getDrugId());
			List<Integer> jj = ck.cxpc(pur.getDrugsYk().getDrugId());
			// 当数组大于0时
			if (jj.size() > 0) {
				System.out.println(jj.get(0));

				int xuyao = pur.getDrugsNumberZc();
				// 打印数组中的批次
				for (Object i : jj) {
					Spenddetai newpur = new Spenddetai();
					System.out.println("需要" + xuyao);
					System.out.println("批次有" + i);
					Ck c = ck.kmcx(pur.getDrugsYk().getDrugId(), (int) i);

					System.out.println("已经查到药品,数量有" + c.getDrugs_Stock_kc());
					// 查询药品
					DrugsYk drug = ddao.findById(pur.getDrugsYk().getDrugId()).get();
					// 库存数量
					int sl = c.getDrugs_Stock_kc();
					if (sl - Math.abs(xuyao) > 0) {
						c.setDrugs_Stock_kc(sl - xuyao);
						// 处方数量
						newpur.setDrugsNumberZc(xuyao);
						System.out.println("一批次就够了");
					} else {
						c.setDrugs_Stock_kc(sl - sl);
						xuyao = sl - xuyao;
						xuyao = Math.abs(xuyao);
						// 处方数量
						newpur.setDrugsNumberZc(sl);
					}
					// 添处方表详情信息
					PrescriptionPx save = pm.findById(pur.getPrescriptionPx().getPrescriptionId()).get();
					System.out.println("处方编号" + save.getPrescriptionId());
					// 处方id
					newpur.setPrescriptionPx(save);
					// 处方备注
					newpur.setDeRemarkZc("无");
					System.out.println("我到了备注");

					// 药品批次
					newpur.setDrugsPc((int) i);
					// 药品id
					newpur.setDrugsYk(drug);
					sm.save(newpur);
					if (sl - Math.abs(xuyao) > 0) {
						break;
					}
				}
			}

		}

	}

	// 部门自动发药
	public void bmzdfy(ChufangVO svo) {

		System.out.println("到了这一步了");
		List<Spenddetai> details = svo.getCfdetails();
		
		for (Spenddetai pur : details) {
			// 根据请领表id查出科室id---修改科室库存数量
			Invoice invoice = im.findById(pur.getInvoice().getInvoiceId()).get();
			Integer did = invoice.getDepartmentsPx().getDepartmentsId();
			Integer drugId = pur.getDrugsYk().getDrugId();
			JSONArray array = tm.findKsKunCun(did, drugId);
			System.out.println("科室--" + did + "药品id--"+drugId+"数量--"+pur.getDrugsNumberZc()+"大小--"+array.size());
			if(array.size()>0) {
				System.out.println("修改--科室库存");
				tm.addAoumnt(did, drugId, pur.getDrugsNumberZc());
			}else {
				System.out.println("新增--科室库存");
				tm.addKsKunCun(did, drugId, pur.getDrugsNumberZc());
			}
			// 根据药品id查询所有批次存入数组
			System.out.println("id" + pur.getDrugsYk().getDrugId());
			List<Integer> jj = ck.cxpc(pur.getDrugsYk().getDrugId());
			// 当数组大于0时
			if (jj.size() > 0) {
				int xuyao = pur.getDrugsNumberZc();
				// 打印数组中的批次
				for (Object i : jj) {
					Spenddetai newpur = new Spenddetai();
					System.out.println("需要" + xuyao+"--批次有" + i);
					Ck c = ck.kmcx(pur.getDrugsYk().getDrugId(), (int) i);
					System.out.println("已经查到药品,数量有" + c.getDrugs_Stock_kc());
					// 查询药品
					DrugsYk drug = ddao.findById(pur.getDrugsYk().getDrugId()).get();
					// 库存数量
					int sl = c.getDrugs_Stock_kc();
					if (sl - Math.abs(xuyao) > 0) {
						c.setDrugs_Stock_kc(sl - xuyao);
						// 处方数量
						newpur.setDrugsNumberZc(xuyao);
						System.out.println("一批次就够了");

					} else {
						c.setDrugs_Stock_kc(sl - sl);
						xuyao = sl - xuyao;
						xuyao = Math.abs(xuyao);
						// 处方数量
						newpur.setDrugsNumberZc(sl);
					}
					// 添处方表详情信息
					Invoice save = im.findById(pur.getInvoice().getInvoiceId()).get();
					System.out.println("处方编号" + save.getInvoiceId());
					// 处方id
					newpur.setInvoice(save);
					// 处方备注
					newpur.setDeRemarkZc("无");
					System.out.println("我到了备注");
					save.setInvoiceState("已领药");
					// 药品批次
					newpur.setDrugsPc((int) i);
					// 药品id
					newpur.setDrugsYk(drug);
					sm.save(newpur);
					if (sl - Math.abs(xuyao) > 0) {
						break;
					}
				}
			}

		}

	}

	public void bmxq(ChufangVO svo) {
		System.out.println("到了这一步了");
		List<Spenddetai> details = svo.getCfdetails();
		Invoice inv = im.findById(details.get(0).getInvoice().getInvoiceId()).get();
		inv.setInvoiceState("已发货");
		im.save(inv);
		System.out.println("到了这一步了" + details.get(0).getInvoice().getInvoiceId());
		// 循环
		for (Spenddetai pur : details) {
			System.out.println("我到了循环了");
			// 添处方表详情信息
			Invoice save = im.findById(pur.getInvoice().getInvoiceId()).get();
			pur.setInvoice(save);

			String bz = pur.getDeRemarkZc();
			pur.setDeRemarkZc(bz);
			System.out.println("我到了备注");
			int id = pur.getDrugsYk().getDrugId();
			System.out.println("id是" + id);
			Ck c = ck.kmpccx(id, pur.getDrugsPc());

			System.out.println(c.getDrugs_Stock_kc() + "--------");
			c.setDrugs_Stock_kc(c.getDrugs_Stock_kc() - pur.getDrugsNumberZc());
			ck.save(c);
			System.out.println("修改成功");

			// 查询药品
			DrugsYk drug = ddao.findById(pur.getDrugsYk().getDrugId()).get();

			// 重新赋值一下药品对象
			pur.setDrugsYk(drug);
			sm.save(pur);
		}
	}
}
